package com.sd.sdk.core.utils;


import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

/**
 * @author xy
 */
public class ApiUrlBase64Util {
    public static byte[] encode(byte[] src) {
        byte[] encode = Base64.getUrlEncoder().encode(src);
        for (int i = encode.length - 1; i >= 0; --i) {
            if (encode[i] != '=') {
                break;
            }
            encode[i] = '.';
        }
        return encode;
    }
    public static String encodeToString(byte[] src) {
        return new String(encode(src), StandardCharsets.UTF_8);
    }
    public static byte[] decode(byte[] src) {
        for (int i = src.length - 1; i >= 0; --i) {
            if (src[i] != '.') {
                break;
            }
            src[i] = '=';
        }
        return Base64.getUrlDecoder().decode(src);
    }
    public static byte[] decode(String src) {
        return decode(src.getBytes(StandardCharsets.UTF_8));
    }

    void name() {
        String s = "{\"totalSize\":\"4\",\"coEnterprises\":[{\"areaId\":null,\"name\":\"全部\",\"pId\":null,\"id\":\"0\",\"type\":null},{\"areaId\":5530,\"name\":\"上海\",\"pId\":\"0\",\"id\":\"310000\",\"type\":\"0\"},{\"areaId\":5530,\"name\":\"上海市\",\"pId\":\"310000\",\"id\":\"310100\",\"type\":\"1\"},{\"areaId\":5530,\"name\":\"长宁区\",\"pId\":\"310100\",\"id\":\"310105\",\"type\":\"2\"}]}|1FD5DD7D5EE5701FEBB27A8B57BBAD47";
        byte[] encode = encode(s.getBytes(StandardCharsets.UTF_8));
        System.out.println(Arrays.toString(encode));
        System.out.println(new String(encode, StandardCharsets.UTF_8));

        byte[] decode = decode(encode);
        System.out.println(Arrays.toString(decode));
        System.out.println(new String(decode, StandardCharsets.UTF_8));
    }
}
